package com.wpw.dream.ajaxparam.action;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.common.json.ParseException;
import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.wpw.dream.common.Student;

@Controller
@RequestMapping("submit")
public class ParamSubmitAction {
	
	
private static Map<String, Map<String, Object>> resMap = new HashMap<>();
	
	@RequestMapping(value = "/testTpl")
	public ModelAndView list(String typeKey) throws Exception {
		
		ModelAndView mv = new ModelAndView();
		if (resMap.containsKey(typeKey)) {
			mv.addObject("typeKey", typeKey);
			mv.addObject("proMap", resMap.get(typeKey));
			mv.setViewName("count");
			return mv;
		}
		
		final String testUrlPre = "http://apinx-test.mysteel.cn/tpl/";
		final String xianshangPre = "http://apinx.mysteel.cn/tpl/";
		String u = this.getClass().getResource("/").getPath();
		
		File folder = new File("D:/stsWorkspace/dream/dream-webapp/dream-web/src/main/webapp/WEB-INF/templates");
		File[] files = folder.listFiles();
		
		List<String> s1List = new ArrayList<>();
		final List<String> fileNames = new ArrayList<>();
		if(files != null && files.length > 0){
			for(File f : files){
				String name = f.getName().trim();
				if(name.endsWith(".html")){
					fileNames.add(name);
				}
			}
		}
		//total,run,List
		final HashMap<String, Object> pro = new HashMap<>();
		pro.put("total", fileNames.size());
		resMap.put(typeKey, pro);
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				@SuppressWarnings("resource")
				WebClient wc = new WebClient(BrowserVersion.CHROME);
				wc.getOptions().setJavaScriptEnabled(false); // 启用JS解释器，默认为true
				wc.getOptions().setCssEnabled(false); // 禁用css支持
				wc.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时，是否抛出异常
				wc.getOptions().setTimeout(10000);
				wc.getOptions().setThrowExceptionOnFailingStatusCode(false);
			
				int i = 1;
				for (String fileName : fileNames) {
					System.out.println("运行第" + i + "个" + fileName);
					try {
						HtmlPage pageTest = wc.getPage(testUrlPre + fileName);
						HtmlPage pageXianShang = wc.getPage(xianshangPre + fileName);
						String test = pageTest.asText();
						String xs = pageXianShang.asText();
						
						pro.put("run", i);
						if (!test.equals(xs)) {
							List<String> list = (List<String>) pro.get("notSame");
							if (CollectionUtils.isEmpty(list)) {
								list = new ArrayList<>();
								list.add(fileName);
								pro.put("notSame", list);
							} else {
								list.add(fileName);
							}
							System.out.println(fileName + "不一致");
						}
					
						i++;
						
					} catch (FailingHttpStatusCodeException e) {
						e.printStackTrace();
					} catch (MalformedURLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
		
		mv.addObject("typeKey", typeKey);
		mv.addObject("proMap", resMap.get(typeKey));
		mv.setViewName("count");
		return mv;
	}
	
	@RequestMapping("/index")
	public String index() {
		return "ajax/index";
	}
	
	@RequestMapping("/ajax/jsonArray")
	@ResponseBody
	public Object jsonArray(@RequestBody Student[]/*List<Student>*/ students) {//数组和List都可以
		System.out.println(Arrays.asList(students));
		if ( CollectionUtils.isNotEmpty(Arrays.asList(students))){
			return "1";
		} else {
			return "0";
		}
	}
	
	@RequestMapping("/ajax/jsonString")
	@ResponseBody
	public Object jsonString(String students, String type) throws ParseException {
		Student[] parse = JSON.parse(students, Student[].class);
		System.out.println(Arrays.asList(parse));
		System.out.println(type);
		if (StringUtils.isNotBlank(type) && CollectionUtils.isNotEmpty(Arrays.asList(students))){
			return "1";
		} else {
			return "0";
		}
	}
	
	@RequestMapping("/formSubmit")
	@ResponseBody
	public Object formSubmit(String[] name, Integer[] age, String type, Integer select, Integer radio, Integer[] checkBox) throws ParseException {
		List<Student> list = new ArrayList<>();
		Student s;
		for (int i = 0; i < age.length; i++) {
			s = new Student();
			s.setName(name[i]);
			s.setAge(age[i]);
			list.add(s);
		}
		System.out.println(list);
		System.out.println("type:" + type + ",select:" + select + ",radio:" + radio + "");
		System.out.println("checkBox:" + Arrays.asList(checkBox));
		return type;
	}

	@RequestMapping("/ajaxfileupload")
	@ResponseBody
	public Object ajaxfileupload(MultipartFile uploadFile, String fileName) {
		System.out.println(uploadFile.getSize());
		Map<String, Object> map = new HashMap<>();
		map.put("msg", fileName);
		return map;
		//String re = "{\"msg\":\""+fileName+"\"}";
		//return re;
	}
	
	@RequestMapping("/ajaxSubmit")
	@ResponseBody
	public Object ajaxSubmit(MultipartFile file, String fileName) {
		System.out.println(file.getSize());
		Map<String, Object> map = new HashMap<>();
		map.put("msg", fileName);
		return map;
	}
}
